﻿using CPPEI.WebApi;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;
using SER = CPPEI.ETL.Domain.Services;
using VIW = CPPEI.ETL.Domain.Views;

namespace CPPEI.ETL.WebApi.AppExtract
{
    /// <summary>
    /// 获取重新执行抽取时的概要信息
    /// </summary>
    [CPPEI.WebApi.AuthGetAction("AppExtract", "GetReexecuteBatch", "1.0")]
    public class GetReexecuteBatchAction : BaseAction<GetReexecuteBatchParams, GetReexecuteBatchResult>
    {
        protected override ActionResult<GetReexecuteBatchResult> OnExecute(ActionContext context, SysParameters sysParams, GetReexecuteBatchParams opParams)
        {
            SER.AppExtractService service = new SER.AppExtractService();

            ListResult<VIW.VBatchDetail> serResult = service.GetReexecuteBatchList(opParams.AppId,
                opParams.RecordId,
                opParams.ServiceType,
                opParams.Source);
            if (serResult.IsSuccess)
            {
                GetReexecuteBatchResult actResult = new GetReexecuteBatchResult();
                for (int index = 0; index < serResult.Data.Count; index++)
                {
                    if (index == 0)
                    {
                        actResult.TaskId = serResult.Data[0].Batch.TaskId;
                    }

                    GetReexecuteBatchResult.Item item = new GetReexecuteBatchResult.Item();
                    item.BatchId = serResult.Data[index].Batch.Id;
                    item.CreatedTime = serResult.Data[index].Batch.CreatedTime;

                    VIW.VBatchDetail batchDetail = serResult.Data[index];
                    for (int pIndex = 0; pIndex < batchDetail.Parameters.Count; pIndex++)
                    {
                        GetReexecuteBatchResult.Item.Parameter parameter = new GetReexecuteBatchResult.Item.Parameter();
                        parameter.Name = batchDetail.SourceParameters[pIndex].Name;
                        parameter.Type = batchDetail.SourceParameters[pIndex].DataType;
                        parameter.Value = batchDetail.Parameters[pIndex].ValueString;
                        item.Parameters.Add(parameter);
                    }
                    actResult.Items.Add(item);
                }
                return new ActionResult<GetReexecuteBatchResult>(actResult);
            }
            else
            {

                return new ActionResult<GetReexecuteBatchResult>(serResult.IsSuccess, serResult.ErrorCode, serResult.Message, null);
            }
        }
    }

    public class GetReexecuteBatchParams
    {
        public Guid AppId { get; set; }
        public Guid RecordId { get; set; }

        /// <summary>
        /// 数据服务类型
        /// </summary>
        public Guid ServiceType { get; set; }

        /// <summary>
        /// 数据源id
        /// </summary>
        public Guid Source { get; set; }
    }

    public class GetReexecuteBatchResult
    {
        public Guid TaskId { get; set; }

        public List<Item> Items = new List<Item>();

        public class Item
        {
            public Guid BatchId { get; set; }

            public DateTime CreatedTime { get; set; }

            public List<Parameter> Parameters = new List<Parameter>();

            public class Parameter
            {
                public string Name { get; set; }

                public string Type { get; set; }

                public string Value { get; set; }
            }
        }
    }
}
